//
//  StudioWorkCollectionViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/9/23.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "StudioWorkCollectionViewController.h"
#import "StudioWorkCollectionViewCell.h"
#import "StudioDetailViewController.h"
#import "AllHeader.h"

#import "WorkModel.h"

#define workURL @"http://arts1.dlab.com.cn/app/getwork"

@interface StudioWorkCollectionViewController ()

// 存储数据源
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation StudioWorkCollectionViewController

static NSString * const reuseIdentifier = @"StudioWorkCell";

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    self.collectionView.backgroundColor = kBackgroundColor;
    self.collectionView.alwaysBounceVertical = YES;
    // Register cell classes
    [self.collectionView registerClass:[StudioWorkCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
}

- (void)setSpaceId:(NSString *)spaceId{
    _spaceId = spaceId;
    if (_spaceId) {
        [self requestData];
    }
}

//请求数据
- (void)requestData
{
    
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *pardic = @{@"spaceId":_spaceId};
    
    [manage GET:workURL parameters:pardic
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
            // 请求成功的时候调用这个block
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            // 请求返回的状态，当status = 1 时有数据
            NSInteger status = [dic[@"status"] integerValue];
            if (status == 1) {
                NSArray *arr = dic[@"list"];
                for (NSDictionary *dict in arr) {
                    // 将数据装换成model类
                    WorkModel *work = [[WorkModel alloc] init];
                    [work setValuesForKeysWithDictionary:dict];
                    [self.dataArr addObject:work];
                }
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
            NSLog(@"画室优秀作品 ===数据请求--%@", dic);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // 请求失败的时候调用调用这个block
            NSLog(@"请求失败");
        }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH - 20, 60);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StudioWorkCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
//    cell.layer.shadowOpacity = 0.5f;
//    cell.layer.shadowRadius = 1.0f;
//    cell.layer.shadowOffset = CGSizeMake(0,1);

    [UIView viewShadowColor:cell];
    
    cell.work = (WorkModel *)_dataArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  //  [self enterSecondDetaileView];
    WorkModel *work = (WorkModel *)_dataArr[indexPath.row];
    if (_studioWorkBlock) {
        _studioWorkBlock(work.Id);
    }
}

/*  ***************** 多层次视图的传值或跳转**************************
 实现跳转到进一步的详情界面， 当遇到层次较多的情况，通常是找到相应的操作对象进行跳转或者传值，在这里是controller，有两种方法：1.通过响应者链，从第一个响应者向window寻找;2.从window开始向最上层的视图找，在这里采用第一种。选择标准一般取决于距离window或者最上层视图的距离
 
 */
- (void)enterSecondDetaileView{
  //  SecondDetailViewController * secondDVC = [[SecondDetailViewController alloc] init];
    UIResponder *responder = [self.view nextResponder];
    while (responder) {
        if ([responder isKindOfClass:[StudioDetailViewController class]]) {
            StudioDetailViewController *studioDVC = (StudioDetailViewController *) responder;
      //      [studioDVC.navigationController  pushViewController:secondDVC animated:YES];
        }
        responder = [responder nextResponder];
    }
    
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
